#include "framebuffer.h"

void draw_circle(int x,int y,int r,int color)
{
    int a,b,num;
    a = 0;
    b = r;
    while(22 * b * b >= r * r)
    {
	fb_put_pixel(x + a,y - b,color);
	fb_put_pixel(x - a,y - b,color);
	fb_put_pixel(x - a,y + b,color);
	fb_put_pixel(x + a,y + b,color);

	fb_put_pixel(x + b,y + a,color);
	fb_put_pixel(x + b,y - a,color);
	fb_put_pixel(x - b,y - a,color);
	fb_put_pixel(x - b,y + a,color);

	a++;
	num = (a * a + b * b) - r*r;
	if(num > 0)
	{
	    b--;
	    a--;
	}
    }
}


void draw_line(int x1,int y1,int x2,int y2,int color)
{
    int dx,dy,e;
    dx=x2-x1;
    dy=y2-y1;

    if (dx>=0) {

	if (dy>=0) {

	    if (dx>=dy) {
		e = dy -dx/2;
		while(x1<=x2)
		{
		    fb_put_pixel(x1,y1,color);
		    if(e>0){y1+=1;e-=dx;}
		    x1+=1;
		    e+=dy;
		} 
	    }else{
		e=dx-dy/2;
		while(y1<=y2)
		{
		    fb_put_pixel(x1,y1,color);
		    if(e>0){x1+=1;e-=dy;}
		    y1+=1;
		    e+=dx;
		} 
	    }

	}else{
	    dy =-dy;
	    if(dx>=dy){

		if (dx>=dy) {
		    e = dy -dx/2;
		    while(x1<=x2)
		    {
			fb_put_pixel(x1,y1,color);
			if(e>0){y1+=1;e-=dx;}
			x1+=1;
			e+=dy;
		    } 
		}else{
		    e=dx-dy/2;
		    while(y1<=y2)
		    {
			fb_put_pixel(x1,y1,color);
			if(e>0){x1+=1;e-=dy;}
			y1+=1;
			e+=dx;
		    } 
		}

	    }
	}

    }else{

	dx=-dx;  //dx=abs(dx)  
	if(dy >= 0) // dy>=0  
	{  
	    if(dx>=dy) // 4/8 octant  
	    {	
		e=dy-dx/2;  
		while(x1>=x2)  
		{	
		    fb_put_pixel(x1,y1,color);  
		    if(e>0){y1+=1;e-=dx;}	   
		    x1-=1;  
		    e+=dy;  
		}	
	    }  
	    else	 // 3/8 octant	
	    {	
		e=dx-dy/2;  
		while(y1<=y2)  
		{	
		    fb_put_pixel(x1,y1,color);  
		    if(e>0){x1-=1;e-=dy;}	   
		    y1+=1;  
		    e+=dx;  
		}	
	    }  
	}  
	else	    // dy<0  
	{  
	    dy=-dy;   // dy=abs(dy)  
	    if(dx>=dy) // 5/8 octant  
	    {	
		e=dy-dx/2;  
		while(x1>=x2)  
		{	
		    fb_put_pixel(x1,y1,color);  
		    if(e>0){y1-=1;e-=dx;}	   
		    x1-=1;  
		    e+=dy;  
		}	
	    }  
	    else	 // 6/8 octant	
	    {	
		e=dx-dy/2;  
		while(y1>=y2)  
		{	
		    fb_put_pixel(x1,y1,color);  
		    if(e>0){x1-=1;e-=dy;}	   
		    y1-=1;  
		    e+=dx;  
		}	
	    }  
	}

    }
}
